function alterdata (g_id) {
	$.ajax({
		type :"POST",
		   url : "ChangeG",
		   dataType :"json",           
		   async : false,
		   data : {
			   "g_id" : g_id
		   },
		   success:function(data){
			   $("#tableSort  tr:not(:first)").empty("");
			   $("#point").text("绩点：" + data[0].point);
			   var s = "";
			   for (var i = 1; i < data.length; i++) 
			   {
				   s += '<tr><td>' + i + '</td><td>' + data[i].c_name + '</td><td>' + data[i].grade + '</td></tr>';
			   }
			   $('#tableSort').append(s);
			   var str =JSON.stringify(data);
		   },
		   error:function(data){
			   $("#tableSort  tr:not(:first)").empty("");
			   alert("返回数据失败");
		   }
	});
}

function alterSclasstable (g_id) {
	$.ajax({
		type :"POST",
		   url : "StuClaTab",
		   dataType :"json",           
		   async : false,
		   data : {
			   "g_id" : g_id
		   },
		   success:function(data){
			   $("#tableSort  tr:not(:first)").empty("");
			   var s = "";
			   for (var i = 0; i < data.length; i++) 
			   {
				   s += '<tr><td>' + data[i].c_name + '</td><td>' + data[i].tea_name + '</td><td>' + data[i].c_time + '</td><td>' + data[i].c_point +'</td></tr>';
			   }
			   $('#tableSort').append(s);
		   },
		   error:function(data){
			   $("#tableSort  tr:not(:first)").empty("");
			   alert("返回数据失败");
		   }
	});
}

function TeaClaTab(year) {
	$.ajax({
		type :"POST",
		   url : "TeaClaTab",
		   dataType :"json",           
		   async : false,
		   data : {
			   "year" : year
		   },
		   success:function(data){
			   $("#tableSort  tr:not(:first)").empty("");
			   for (var i = 0; i < data.length; i++) 
			   {
				   var s = "";
				   var n = "";
				   var m = "";
				   s = '<tr><td>' + data[i].c_name + '</td><td>' + data[i].c_time + '</td><td>' + data[i].c_point + '</td>';
				   n = '<td>' + data[i].cla_name +'</td>';
				  
				   if(data[i].g_id%2 == 0)
					   {
					   m = '<td>春季学期</td></tr>';
					   }
				   else{
					   m = '<td>秋季学期</td></tr>';
				   }
				   s = s+n+m
				   $('#tableSort').append(s);
			   }
			   
		   },
		   error:function(data){
			   $("#tableSort  tr:not(:first)").empty("");
			   alert("返回数据失败");
			   alert(data);
		   }
	});
}

function alterTdata (){
	var cla_id = $("#cla").val();
	var c_id = $("#cou").val();
	var year = $("#year").val();
	$.ajax({
		type :"POST",
		   url : "TChangeCla",
		   dataType :"json",           
		   async : false,
		   data : {
			   "cla_id" : cla_id,
			   "c_id" : c_id,
			   "year" : year
		   },
		   success:function(data){
			   $("#tableSort  tr:not(:first)").empty("");
			   var s = "";
			   for (var i = 0; i < data.length; i++) 
			   {
				   s += '<tr><td>' + i+1 + '</td><td>' + data[i].stu_name + '</td><td>' + data[i].grade + '</td></tr>';
			   }
			   $('#tableSort').append(s);
			   
	           
		   },
		   error:function(data){
			   $("#tableSort  tr:not(:first)").empty("");
			   alert("返回数据失败");
		   }
	});
}
function alterMdata(){
	var c_id = $("#cou").val();
	var year = $("#year").val();
	alert(c_id);
	if(c_id != 0||year != 0)
		{
		$.ajax({
			type :"POST",
			   url : "OOTChange",
			   dataType :"json",           
			   async : false,
			   data : {
				   "c_id" : c_id,
				   "year" : year
			   },
			   success:function(data){
				   $("#tableSort  tr:not(:first)").empty("");
				   var s = "";
				   for (var i = 0; i < data.length; i++) 
				   {
					   s += '<tr><td>' + i+1 + '</td><td>' + data[i].tea_name + 
					   '</td><td>' + data[i].cla_name + '</td><td>' + data[i].ave_sco + 
					   '</td><td>' + data[i].passrate + '%' +
					   '</td><td>' + data[i].goodrate + '%' + '</td></tr>';
				   }
				   $('#tableSort').append(s);
				   
		           
			   },
			   error:function(data){
				   $("#tableSort  tr:not(:first)").empty("");
				   alert("返回数据失败");
			   }
		});
		}
	
}

function alterMCladata(){
	var g_id = $("#uc_02").val();
	var year = $("#year").val();
		$.ajax({
			type :"POST",
			   url : "CCChange",
			   dataType :"json",           
			   async : false,
			   data : {
				   "g_id" : g_id,
				   "year" : year
			   },
			   success:function(data){
				   $("#tableSort  tr:not(:first)").empty("");
				   $("#tableSort tr th:gt(1)").remove();
				   var s = "";
				   var n = "";
				   for (var i = 0; i < data[0].length; i++) 
				   {
					   
					   s += '<th onclick="sortTable("tableSort",i)" title="点击排序">' + data[0][i].c_name + '（平均分）' + '</th>';
				   }
				   $('#ft').append(s);
				   for (var i = 1; i < data.length; i++) 
				   {
					   var m = "";
					   var x = i;
					   n += '<tr><td>' + x + '</td><td>' + data[i].cla_name +'</td>';
					   for (var j = 0; j < data[i].ave_sco_all.length; j++) 
					   {
						   m += '<td>' + data[i].ave_sco_all[j] +'</td>';
					   }
					   n = n + m + '</tr>'
				   }
				   $('#tableSort').append(n);
			   },
			   error:function(data){
				   $("#tableSort  tr:not(:first)").empty("");
				   alert("暂无数据");
			   }
		});
	
}

function ClassgetG_id(){
	var cla_id = $("#cla").val();
	
		$.ajax({
			type :"POST",
			   url : "GetGForClass",
			   dataType :"json",           
			   async : false,
			   data : {
				   "cla_id" : cla_id,
			   },
			   success:function(data){
				   $("#tableSort  tr:not(:first)").empty("");
				   $("#tableSort tr th:gt(2)").remove();
				   $("#uc_02 option[value='"+data[0].g_id+"']").attr("selected","selected"); 
				   var s = "";
				   var n = "";
				   for (var i = 0; i < data[0].c_name.length; i++) 
				   {
					   if(data[1].c_name[i] != null)
						   {
						   s += '<th onclick="sortTable("tableSort",i+3)" title="点击排序">' + data[1].c_name[i]  + '</th>';
						   }
					   
				   }
				   $('#ft').append(s);
				   for (var i = 0; i < data.length; i++) 
				   {
					   var m = "";
					   var x = i+1;
					   n += '<tr><td>' + x + '</td><td>' + data[i].stu_name +'</td><td>'+ data[i].point +'</td>';
					   for (var j = 0; j < data[0].c_name.length; j++) 
					   {
						   if(data[1].c_name[j] != null)
							   {
							   		if(data[i].grade[j] != -1)
							   		{
							   			m += '<td>' + data[i].grade[j] +'</td>';
							   		}
							   		else
							   		{
							   			m += '<td>暂无成绩</td>';
							   		}
							   		
							   }
						   
					   }
					   n = n + m + '</tr>'
				   }
				   $('#tableSort').append(n);
			   },
			   error:function(data){
				   $("#tableSort  tr:not(:first)").empty("");
				   alert("暂无数据");
			   }
		});
	
}

function ClassStuChangeG(){
	var cla_id = $("#cla").val();
	var g_id = $("#uc_02").val();
		$.ajax({
			type :"POST",
			   url : "ClassStuChangeG",
			   dataType :"json",           
			   async : false,
			   data : {
				   "cla_id" : cla_id,
				   "g_id":g_id,
			   },
			   success:function(data){ 
				   $("#tableSort  tr:not(:first)").empty("");
				   $("#tableSort tr th:gt(2)").remove();
				   var s = "";
				   var n = "";
				   for (var i = 0; i < data[0].c_name.length; i++) 
				   {
					   if(data[1].c_name[i] != null)
						   {
						   s += '<th onclick="sortTable("tableSort",i+3)" title="点击排序">' + data[1].c_name[i]  + '</th>';
						   }
					   
				   }
				   $('#ft').append(s);
				   for (var i = 0; i < data.length; i++) 
				   {
					   var m = "";
					   var x = i+1;
					   n += '<tr><td>' + x + '</td><td>' + data[i].stu_name +'</td><td>'+ data[i].point +'</td>';
					   for (var j = 0; j < data[0].c_name.length; j++) 
					   {
						   if(data[1].c_name[j] != null)
							   {
							   		if(data[i].grade[j] != -1)
							   		{
							   			m += '<td>' + data[i].grade[j] +'</td>';
							   		}
							   		else
							   		{
							   			m += '<td>暂无成绩</td>';
							   		}
							   		
							   }
						   
					   }
					   n = n + m + '</tr>'
				   }
				   $('#tableSort').append(n);
			   },
			   error:function(data){
				   $("#tableSort  tr:not(:first)").empty("");
				   alert("暂无数据");
			   }
		});
	
}

function sortTable(table, idx) {
  var otable = document.getElementById(table),
    otody = otable.tBodies[0],
    otr = otody.rows,
    tarr = [];
  for (var i = 1; i < otr.length; i++) {
    tarr[i - 1] = otr[i];
  };
  // console.log(tarr);
  if (otody.sortCol == idx) {
    tarr.reverse();
  } else {
    tarr.sort(function (tr1, tr2) {
      var value1 = tr1.cells[idx].innerHTML;
      var value2 = tr2.cells[idx].innerHTML;
      if (!isNaN(value1) && !isNaN(value2)) {
        return value1 - value2;
      } else {
        return value1.localeCompare(value2);
      }
    })
  }
  var fragment = document.createDocumentFragment();
  for (var i = 0; i < tarr.length; i++) {
    fragment.appendChild(tarr[i]);
  };
  otody.appendChild(fragment);
  otody.sortCol = idx;
}
//鎷栧姩
function Drag(table) {
  var ochek = document.getElementById("chenkbox"),
    otable = document.getElementById(table),
    otody = otable.tBodies[0],
    oth = otody.getElementsByTagName("th"),
    otd = otody.getElementsByTagName("td"),
    box = document.getElementById("box"),
    arrn = [];
  for (var i = 0; i < otd.length; i++) {
    otd[i].onmousedown = function (e) {
      var e = e || window.event,
        target = e.target || e.srcElement,
        thW = target.offsetWidth,
        maxl = ochek.offsetWidth - thW,
        rows = otable.rows,
        ckL = ochek.offsetLeft,
        disX = target.offsetLeft,
        _this = this,
        cdisX = e.clientX - ckL - disX;
      for (var i = 0; i < rows.length; i++) {
        var op = document.createElement("p");
        op.innerHTML = rows[i].cells[this.cellIndex].innerHTML;
        box.appendChild(op);
      };
      for (var i = 0; i < oth.length; i++) {
        arrn.push(oth[i].offsetLeft);
      };
      console.log(arrn);
      box.style.display = "block";
      box.style.width = thW + "px";
      box.style.left = disX + "px";
      //鏈畬鎴� 杩樻湁浜嬩欢娌″啓銆�
      document.onmousemove = function (e) {
        var e = e || window.event,
          target = e.target || e.srcElement,
          thW = target.offsetWidth;
        box.style.top = 0;
        box.style.left = e.clientX - ckL - cdisX + "px";
        if (box.offsetLeft > maxl) {
          box.style.left = maxl + "px";
        } else if (box.offsetLeft < 0) {
          box.style.left = 0;
        }
        document.onselectstart = function () {
          return false
        };
        window.getSelection ? window.getSelection().removeAllRanges() : doc.selection.empty();
      }
      document.onmouseup = function (e) {
        var e = e || window.event,
          opr = box.getElementsByTagName("p"),
          oboxl = box.offsetLeft + cdisX;
        for (var i = 0; i < arrn.length; i++) {
          if (arrn[i] < oboxl) {
            var index = i;
          }
        };
        for (var i = 0; i < rows.length; i++) {
          rows[i].cells[_this.cellIndex].innerHTML = "";
          rows[i].cells[_this.cellIndex].innerHTML = rows[i].cells[index].innerHTML;
          rows[i].cells[index].innerHTML = "";
          rows[i].cells[index].innerHTML = opr[i].innerHTML;
        };
        box.innerHTML = "";
        arrn.splice(0, arrn.length);
        box.style.display = "none";
        document.onmousemove = null;
        document.onmouseup = null;
        document.onselectstart = function () {
          return false
        };
      }

    }
  };

}
Drag("tableSort");





